package com.lcicl.reggie.controller;
/*
  Date: 2023/10/21
  Author: Luochao
  Description:
*/

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lcicl.reggie.common.R;
import com.lcicl.reggie.entity.User;
import com.lcicl.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;


    @PostMapping("/login")
    public R<String> login(HttpServletRequest request, @RequestBody User user){

        //查询用户是否存在
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhone, user.getPhone());
        User userExist = userService.getOne(queryWrapper);

        //不存在添加用户
        if (userExist == null) {
            userService.save(user);
            userExist = userService.getOne(queryWrapper);
        }

        //登录
        request.getSession().setAttribute("user", userExist.getId());
        System.out.println(user.getPhone()+"登录成功");

        return R.success("登录成功");
    }

    @PostMapping("/loginout")
    public R<String> logout(HttpServletRequest request){

        //退出登录
        request.getSession().removeAttribute("user");

        return R.success("退出成功");
    }




}
